﻿@model XStudio.Framework.Support.Entity.Authorize.RoleEntity
@using XStudio.Core.Ext
@using XStudio.Framework.Util
<script type="text/javascript">

    var PartTree = {};


    //隐藏显示下级
    PartTree.TitleClick = function (titleNode, ignore) {
        if (ignore != true) {
            //过滤链接点击事件
            var e = Common.GetEvent();
            var srcElement = e.srcElement || e.target;
            if (srcElement.tagName.toLowerCase() == "a") {
                return;
            }
        }

        var parentLevel = parseInt($(titleNode).attr("level"), 10);
        if ($(titleNode).hasClass("tgd-l")) {
            $(titleNode).next("p").hide();

            //遍历所有当前h5下的所有h5标签
            $.each($(titleNode).nextAll("h5"), function (i, item) {
                //如果是同级则跳出遍历
                var itemLevel = parseInt($(item).attr("level"), 10);
                if (itemLevel <= parentLevel) {
                    return false;
                }
                $(item).hide();
                $(item).next("p").hide();
            });
            //样式转换
            $(titleNode).toggleClass("tgd-l");
        }
        else {
            //样式转换
            $(titleNode).toggleClass("tgd-l");

            $(titleNode).next("p").show();
            //            if (!$(titleNode).next("p").attr("empty")) {
            //                $(titleNode).next("p").show();
            //            }

            //遍历所有当前h5下的所有h5标签
            $.each($(titleNode).nextAll("h5"), function (i, item) {
                //如果是同级则跳出遍历
                var itemLevel = parseInt($(item).attr("level"), 10);
                if (itemLevel <= parentLevel) {
                    return false;
                }
                //父父节点非折叠且可见或本非折叠或是当前的下一层则显示
                if (($(item).prevAll("h5[level='" + (itemLevel - 1) + "']:first").hasClass("tgd-l")) && ($(item).prevAll("h5[level='" + (itemLevel - 1) + "']:first").css("display") != "none")) {
                    $(item).show();
                }

                //判断子项不为空,且隐藏前当前节点为显示状态则再次显示
                if (!$(item).next("p").attr("empty") && $(item).hasClass("tgd-l") && $(item).css("display") != "none") {
                    $(item).next("p").show();
                }
            });
        }
    }

    //重新呈现页面
    var _IsFirstRender = true;
    PartTree.RenderPart = function () {
        _IsFirstRender = true;
        $.each($("h5[level='1']"), function (i, item) {
            PartTree.RenderLink($(item).children("a"));
        });
        _IsFirstRender = false;

        //初始化缩进
        var levelString = ["", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"];
        $.each(levelString, function (i, item) {
            if (item != null && item != "") {
                $("." + item).css({ 'margin-left': (2 * i) + 'em' }).next('.ct').css({ 'margin-left': (2 * i + 1) + 'em' });
            }
        });
    }

    //父节点点击事件(所有有子节点的节点)
    PartTree.LinkNodeClick = function (link) {
        var selectMode = parseInt($(link).attr("selectemode"));
        var childIsChecked = true;

        switch (selectMode) {
            //全部未选中-> 仅父节点选中                                                                                   
            case 0:
                {
                    PartTree.RenderLinkCss($(link), 1);
                    childIsChecked = false;
                }
                break;

            //仅父节点选中  -> 全部选中                                                                                     
            case 1:
                {
                    PartTree.RenderLinkCss($(link), 3);
                }
                break;

            //子节点部分选中  -> 全部选中                                                                                     
            case 2:
                {
                    PartTree.RenderLinkCss($(link), 3);
                }
                break;

            //3全部选中  -> 全部未选中                                                                                     
            default:
                {
                    PartTree.RenderLinkCss($(link), 0);
                    childIsChecked = false;
                }
                break;
        }

        if (selectMode != 0) {
            PartTree.OperateChildNodes(link, childIsChecked);
        }

        if (parseInt($(link).parent("h5").attr("level"), 10) != 1) {
            PartTree.RenderLink($(link).parent("h5").prevAll("h5[level='1']:first").children("a"));
        }
    }

    //复选框批量处理，选中取消选中
    PartTree.OperateChildNodes = function (link, isChecked) {
        var parentLevel = parseInt($(link).parent("h5").attr("level"), 10);

        //遍历所有当前h5下的所有h5标签的所有所有多选框
        $.each($(link).parent("h5").nextAll("h5"), function (i, item) {
            //如果是同级则跳出遍历
            var itemLevel = parseInt($(item).attr("level"), 10);
            if (itemLevel <= parentLevel) {
                return false;
            }

            //当前h5下链接样式设置
            PartTree.RenderLinkCss($(item).children("a"), isChecked ? 3 : 0);

            //所有所有多选框
            $.each($(item).next("p").find(":checkbox"), function (i, input) {
                input.checked = isChecked;
            });
        });

        //遍历当前h5下所有多选框
        var childInputs = $(link).parent("h5").next("p").find(":checkbox");
        $.each(childInputs, function (i, item) {
            item.checked = isChecked;
        });
    }

    //呈现父节点链接样式
    PartTree.RenderLinkCss = function (link, selectMode) {
        //链接样式设置
        link.removeClass();
        link.attr("selectemode", selectMode);
        link.addClass(selectMode == 3 ? "btn-checkall" : (selectMode == 0 ? "btn-checknone" : "btn-check"));
        if (selectMode == 1) {
            $(link).parent("h5").find(":checkbox").attr("checked", "checked");
        }
        else {
            $(link).parent("h5").find(":checkbox").removeAttr("checked");
        }
        if (_IsFirstRender) {
            if ((selectMode == 1 || selectMode == 2)) {
                $(link).parent("h5").removeClass("tgd-l");
                PartTree.TitleClick($(link).parent("h5"), true);
            }
            else {
                $(link).parent("h5").addClass("tgd-l");
                PartTree.TitleClick($(link).parent("h5"), true);
            }
        }
    }

    //子项(多选框列表)点击事件
    PartTree.ChildNodeClick = function (node) {
        var firstLink;
        if (parseInt($(node).prevAll("h5:first").attr("level"), 10) == 1) {
            PartTree.RenderLink($(node).prevAll("h5:first").children("a"));
        }
        else {
            PartTree.RenderLink($(node).prevAll("h5[level='1']:first").children("a"));
        }
    }

    //子节点修改,呈现连接选中样式
    PartTree.RenderLink = function (link) {
        var parentLevel = parseInt($(link).parent("h5").attr("level"), 10);
        var checkedCount = 0;
        var nodeCount = 0;
        //当前级别子工节点
        checkedCount = $(link).parent("h5").next("p").find("input:checked").length;
        nodeCount = $(link).parent("h5").next("p").find(":checkbox").length;
        var currSelectMode = (checkedCount == nodeCount) ? 3 : checkedCount > 0 ? 2 : 0;
        if (nodeCount == 0 || currSelectMode == 0) {
            currSelectMode = $(link).parent("h5").find(":checkbox").attr("checked") ? 1 : 0;
        }
        var childSelectMode = -1;

        //遍历所有当前h5下的所有h5标签的所有所有多选框
        $.each($(link).parent("h5").nextAll("h5"), function (i, item) {
            //如果是同级则跳出遍历
            var itemLevel = parseInt($(item).attr("level"), 10);
            if (itemLevel <= parentLevel) {
                return false;
            }

            //子节点链接选中模式
            var temp = PartTree.RenderLink($(item).children("a"));
            if (childSelectMode > -1) {
                if (temp == 1 || temp == 2 || childSelectMode == 2 || (temp == 3 && childSelectMode != 3) || (temp != 3 && childSelectMode == 3) || (temp == 0 && childSelectMode != 0)) {
                    childSelectMode = 2;
                }
                else {
                    childSelectMode = temp;
                }
            }
            else {
                childSelectMode = temp;
            }
        });

        //有子节点,进行组合判断目前选中类型
        if (nodeCount == 0 && childSelectMode > 0) {
            currSelectMode = childSelectMode;
        }
        else if (childSelectMode > -1 && (childSelectMode == 1 || childSelectMode == 2 || currSelectMode == 2 || (childSelectMode == 3 && currSelectMode != 3) || (childSelectMode != 3 && currSelectMode == 3))) {
            currSelectMode = 2;
        }

        //        //如果当前节点没有子项,隐藏当前p标签
        //        if (nodeCount == 0) {
        //            $(link).parent("h5").next("p").attr("empty", "true");
        //            $(link).parent("h5").next("p").hide();
        //        }

        //当前h5下链接样式设置
        PartTree.RenderLinkCss(link, currSelectMode);

        return currSelectMode;
    }

    PartTree.Save = function (e) {
        var frm = $(e).parents("form").first();
        if (!$(frm).valid())
            return;

        $.post($(frm).attr("action"), $(frm).serialize(), function (data) {
            if (data.IsOK) {
                //刷新列表
                $('#tbRoleList').datagrid('reload');

                //关闭选项卡
                Common.CloseTab();
            }
            else
                alert(data.Description);
        });
    }

    $(function () {

        // jQuery.validator增加Code验证
        jQuery.validator.addMethod("code", function (value, element) {
            return this.optional(element) || (/^[a-zA-Z0-9_]+$/.test(value));
        }, "请输入英文、数字");

        var v = $('#frmPartTree').validate({
            rules: {
                RoleName: { required: true },
                RoleCode: { required: true, code: true }
            },
            messages: {
                RoleName: { required: "必填项！" },
                RoleCode: { required: "必填项！" }
            }
        });
        PartTree.RenderPart();
    });
    
</script>
<form name="frmPartTree" id="frmPartTree" method=post action="@Url.Action(ViewContext.RouteData.Values["Action"].ToString())" >
<h6 class="tg-left tt-block title tgd-l">
    基础信息</h6>
<div style="padding-left: 1.5em;">
    <table class="tbl-input" cellspacing="0">
        <colgroup>
            <col style="width: 150px;" />
            <col class="w5" />
            <col />
        </colgroup>
        <tbody>
            <tr>
                <th>
                    角色类型：
                </th>
                <td>
                    @{
                        var isEdit = string.Equals(ViewContext.RouteData.Values["Action"].ToString(), "Edit", StringComparison.InvariantCultureIgnoreCase);}
                    @if (isEdit)
                    {
                        @Html.DropDownListFor(t => t.RoleTypeID, (SelectListItem[])ViewBag.RoleTypes, new { width = "50px", disabled = "disabled" })
                    }
                    else
                    {
                        @Html.DropDownListFor(t => t.RoleTypeID, (SelectListItem[])ViewBag.RoleTypes, new { width = "50px" })
                    }
                </td>
                <td class="slight">
                    <span class="btn-infomust">必填项</span>
                </td>
            </tr>
            <tr>
                <th>
                    角色名称：
                </th>
                <td>
                    @Html.TextBoxFor(t => t.RoleName, new { style = "width:200px", maxlength = "50" })
                </td>
                <td class="slight">
                    <span class="btn-infomust">必填项</span>
                </td>
            </tr>
            @if (Request["Admin"] == "1")
            {
                <tr>
                    <th>
                        角色代码：
                    </th>
                    <td>
                        @Html.TextBoxFor(t => t.RoleCode, new { style = "width:200px", maxlength = "50" })
                    </td>
                    <td class="slight">
                        <span class="btn-infomust">必填项，允许输入英文、数字</span>
                    </td>
                </tr>
            }
            <tr>
                <th>
                    备注：
                </th>
                <td>
                    @Html.TextAreaFor(t => t.Remark, new { style = "width:200px", rows = 4 })
                </td>
                <td class="slight">
                    <span class="btn-info">选填项</span>
                </td>
            </tr>
            <tr>
                <th scope="row">
                </th>
                <td>
                    @Html.CheckBox("StateCodeID", Model.StateCodeID == 1, new { value = "1" })
                    <label for="StateCodeID">
                        是否启用</label>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<h6 class="tg-left tt-block title tgd-l">
    角色模块权限</h6>
<div style="padding-left: 1.5em; padding-right: .5em;">
    @{
        var parts = (List<TreeData>)ViewBag.Parts;
    }
    @foreach (var treeData in parts)
    {
        Html.RenderPartial("PartTreeItem", treeData);
    }
</div>
<p class="op" style="text-align: center; margin-top: 10px;">
    <input type="button" id="btnSave" onclick="PartTree.Save(this);" class="btn-8" value="保存" />
    <input type="button" value="返回" onclick="Common.CloseTab(this);" class="btn-8" />
</p>
</form>
